<?php
class Admin_IndexController extends Apps_Controller_Action_Admin {
	public function indexAction() {
	    //Dashboard

	    //count tour
        $this->view->countTour            = $this->countStatistic('tour', false);
        $this->view->countTourPublic      = $this->countStatistic('tour');
        $this->view->countTourCategory    = $this->countStatistic('tour_category');
        $this->view->countTourStyle       = $this->countStatistic('tour_style');

        $this->view->countCruise          = $this->countStatistic('cruise', false);
        $this->view->countCruisePublic    = $this->countStatistic('cruise');

        $this->view->countHotel           = $this->countStatistic('hotel', false);
        $this->view->countHotelPublic     = $this->countStatistic('hotel');

        $this->view->countCustomer        = $this->countStatistic('customer');
        $this->view->countOrder           = $this->countStatistic('order');

        $this->view->countEnquire         = $this->countStatistic('enquire', false);
        $this->view->countEnquireNew      = $this->countStatistic('enquire');
        $this->view->countContact         = $this->countStatistic('contact', false);
        $this->view->countContactNew      = $this->countStatistic('contact');
	}

	public function countStatistic($table, $isPublic = true) {
        $q = $this->_dbAdapter
                  ->select()
                  ->from($table, array('count' => 'COUNT(id)'))
                  ->where($table.'.status != -1');

        if($isPublic) {
            $q->where('status =?', 1);
        }
        if($d = $q->query()->fetch()) {
            return $d['count'];
        } else return false;
	}

	public function ajaxAction() {
		// XmlHttpRequest resolve
		$rq = $this->_request;
		if($rq->isXmlHttpRequest() && $ajax = $rq->getParam('ajax')) {
			return $this->_forward('ajax-' . $ajax);
		}

		$this->_forward('index');
	}

	// Ajax/ Region
	public function ajaxListRegionAction() {
		$this->ajaxOnly();

		$r = array();

		if($p = $this->_getParam('p')) {
			$tb = new M_DbTable_Region();
			$d  = $tb->listRegionByCountryId($p);

			if($d) {
				$dMapper = new M_Tool_DataMapper();
				$r       = $dMapper->mapKeyValue($d, 'id', 'en_name');
				array_unshift($r, 'City - Province');
			}
		} else {
			$r[0] = 'Có lỗi xảy ra, vui lòng chọn lại Tỉnh/Thành phố';
		}
		echo Zend_Json::encode($r);
	}
	// Ajax Area
	public function ajaxListAreaAction() {
		$this->ajaxOnly();

		$r = array();
		if($p = $this->_getParam('p')) {
			$tb = new M_DbTable_Area();
			$d  = $tb->listAreaByCountryId($p);

			if($d) {
				$dMapper = new M_Tool_DataMapper();
				$r       = $dMapper->mapKeyValue($d, 'id', 'en_name');
			}
		} else {
			$r[0] = 'Có lỗi xảy ra, vui lòng chọn lại Tỉnh/Thành phố';
		}
		echo Zend_Json::encode($r);
	}
	public function nothing() {
		$v = new Zend_Validate_Db_NoRecordExists(array('table' => 'user', 'field' => 'id'));
		$uM = new M_DbTable_User(); 
	}
	
	// Ajax Area destination
	public function ajaxListAreaDestinationAction() {
		$this->ajaxOnly();

		$r = array();
		if($p = $this->_getParam('p')) {
			$tb = new M_DbTable_AreaDestination();
			$d  = $tb->listAreaByCountryId($p);

			if($d) {
				$dMapper = new M_Tool_DataMapper();
				$r       = $dMapper->mapKeyValue($d, 'id', 'en_name');
			}
		} else {
			$r[0] = 'Có lỗi xảy ra, vui lòng chọn lại Tỉnh/Thành phố';
		}
		echo Zend_Json::encode($r);
	}
	
	public function nothingAction() {
		$v = new Zend_Validate_Db_NoRecordExists(array('table' => 'user', 'field' => 'id'));
		$uM = new M_DbTable_User(); 
	}
}